﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page
{
    private DBArticles _mostReadableArt = new DBArticles();
    private DBRecommended _recommendedArt = new DBRecommended();
    private DBArticles _newArticles = new DBArticles();
    
    public string MenuMostReadableArt = "";
    public string MenuRecommended = "";
    public string NewArt1 = "";
    public string NewArt2 = "";
    public string NewArt3 = "";
    public string NewArt4 = "";
    public string NewArt5 = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["isLogIn"].Equals("true"))
        {
            Login.Visible = false;
            TxtLogin.Visible = false;
            Password.Visible = false;
            TxtPassword.Visible = false;
            Log_In.Visible = false;
            LblError.Visible = false;
            LinkRegistration.Visible = false;
            LblIsLogIn.Visible = true;
            LblIsLogIn.Text = "Jesteś zalogowany jako " + Session["login"];
            Log_Out.Visible = true;
        }
        else
        {
            Login.Visible = true;
            TxtLogin.Visible = true;
            Password.Visible = true;
            TxtPassword.Visible = true;
            Log_In.Visible = true;
            LinkRegistration.Visible = true;
            Log_Out.Visible = false;
            LblError.Visible = false;
            LblIsLogIn.Visible = false;
        }


        _mostReadableArt.ClearArticles();
        _recommendedArt.ClearRecommended();
        _newArticles.ClearArticles();

        //SQL
        //najczesciej czytane
        string sql1 = "SELECT TOP 10 * FROM Articles ORDER BY NumberOfClicks DESC";
        //polecane
        string sql2 = "SELECT * FROM Recommended WHERE SECTION LIKE 'Zdrowie'";
        //najnowsze artykuly
        string sql3 = "SELECT * FROM Articles ORDER BY InsertDate DESC";


        //najczesciej czytane - Code

        SqlDataReader reader1 = DataBase.SelectData(sql1);
        while (reader1.Read())
        {
            Article art = new Article(reader1.GetInt32(0), reader1.GetString(1), reader1.GetString(2),
                                      reader1.GetString(3), reader1.GetString(4), reader1.GetDateTime(5),
                                      reader1.GetInt32(6), reader1.GetString(7));
            _mostReadableArt.AddArticle(art);
        }
        reader1.Close();
        _setMostReadable();


        //polecane - Code

        SqlDataReader reader2 = DataBase.SelectData(sql2);
        while (reader2.Read())
        {
            RecommendedArt rcmd = new RecommendedArt(reader2.GetInt32(0), reader2.GetString(1), reader2.GetString(2),
                                               reader2.GetString(3), reader2.GetString(4));
            _recommendedArt.AddRecommended(rcmd);
        }
        reader2.Close();
        _setRecommended();


        //nowe - Code

        SqlDataReader reader3 = DataBase.SelectData(sql3);
        while (reader3.Read())
        {
            Article art = new Article(reader3.GetInt32(0), reader3.GetString(1), reader3.GetString(2),
                                      reader3.GetString(3), reader3.GetString(4), reader3.GetDateTime(5),
                                      reader3.GetInt32(6), reader3.GetString(7));
            _newArticles.AddArticle(art);
        }
        reader3.Close();
        _setNews();
    }

       
    //wstawianie artykułów do poszczegolnych modulow
    private void _setMostReadable()
    {
        List<string> titles = _mostReadableArt.GetArticlesTitles();
        for(int i = 0; i < titles.Count; i++)
        {
            MenuMostReadableArt += "<a href=''>";
            MenuMostReadableArt += titles.ElementAt(i);
            MenuMostReadableArt += "</a> <br>";
        }
    }
   
    private void _setRecommended()
    {
        List<string> rcmd = _recommendedArt.GetRecommended();
        for (int i = 0; i < rcmd.Count; i++)
        {
            MenuRecommended += rcmd.ElementAt(i);
            MenuRecommended += "<br>";
        }
    }

    private void _setNews()
    {
        List<string> newArt = _newArticles.GetArticlesTitles();
        NewArt1 += newArt.ElementAt(0);
        NewArt2 += newArt.ElementAt(1);
        NewArt3 += newArt.ElementAt(2);
        NewArt4 += newArt.ElementAt(3);
        NewArt5 += newArt.ElementAt(4);
    }

    protected void Log_In_Click(object sender, EventArgs e)
    {
        string login = TxtLogin.Text;
        string password = TxtPassword.Text;

        var sql = "SELECT COUNT(*) FROM Users WHERE login LIKE '" + login + "' AND password LIKE '" + password + "'";

        SqlDataReader readerUsers = DataBase.SelectData(sql);

        readerUsers.Read();

        if (login.Equals("admin") & password.Equals("admin"))
        {
            Session["login"] = login;
            Session["isLogIn"] = "true";
            readerUsers.Close();
            Response.Redirect("AdministrationPanel.aspx");
        }

        //moze byc kilka takich samych loginow i hasel
        if (readerUsers.GetInt32(0) >= 1)
        {
            Session["login"] = login;
            Session["isLogIn"] = "true";
            readerUsers.Close();
            Response.Redirect("UserProfile.aspx");
            Login.Visible = false;
            TxtLogin.Visible = false;
            Password.Visible = false;
            TxtPassword.Visible = false;
            Log_In.Visible = false;
            LblError.Visible = false;
            LinkRegistration.Visible = false;
            LblIsLogIn.Visible = true;
            LblIsLogIn.Text = "Jesteś zalogowany jako " + Session["login"];
            Log_Out.Visible = true;
        }
        else
        {
            readerUsers.Close(); 
            Login.Visible = true;
            TxtLogin.Visible = true;
            Password.Visible = true;
            TxtPassword.Visible = true;
            Log_In.Visible = true;
            LinkRegistration.Visible = true;
            Log_Out.Visible = false;
            LblError.Visible = true;
            TxtLogin.Text = "";
            TxtPassword.Text = "";
        }

        readerUsers.Close();
    }

    protected void Log_Out_Click(object sender, EventArgs e)
    {
        Session["isLogIn"] = "false";
        Response.Redirect("Default.aspx");
        Login.Visible = true;
        TxtLogin.Visible = true;
        Password.Visible = true;
        TxtPassword.Visible = true;
        Log_In.Visible = true;
        LinkRegistration.Visible = true;
        Log_Out.Visible = false;
        LblIsLogIn.Visible = false;
        LblIsLogIn.Text = "";
        TxtLogin.Text = "";
        TxtPassword.Text = "";
    }

    protected void LinkRegistration_Click(object sender, EventArgs e)
    {
        Response.Redirect("Registration.aspx");
    }

    protected void BtnSearch_Click(object sender, EventArgs e)
    {
        //string url = String.Format("~/SearchResult.aspx?SearchField={0}", TxtSearch.Text);
        //Response.Redirect(url);
        string searchWord = TxtSearch.Text;
        if (!searchWord.Equals(""))
        {
            Response.Redirect("SearchResult.aspx?SearchField=" + searchWord);
        }
    }

    protected void LinkLogo_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
}

 